/**
 * Toolbar containing the search button.
 */
package am.fp.toolbars
{ 
  import fl.controls.Button;                    
  import flash.events.Event;
  
  import am.fp.Toolbar;
  import am.fp.FPDispatcher;
  import am.fp.events.SidePaneEvent;
  
  /**
   * Toolbar containing the search button.
   */
  public class Search extends Toolbar
  { 
    private var m_btnSearch: Button;
    
    /**
     * Constructor. Add button and connect listeners.
     */
    public function Search()
    {
      m_btnSearch = addButton("SearchIcon");
      m_btnSearch.toggle = true;
      m_btnSearch.selected = false;

      m_btnSearch.addEventListener(Event.CHANGE, onSearch, false, 0, true);
      FPDispatcher.instance.addEventListener(SidePaneEvent.CHANGE, onSidePaneChange, false, 0, true);
    }
      
    /**
     * Dispatch event to show/hide the search pane.
     */
    protected function onSearch(e: Event): void
    {                            
      FPDispatcher.instance.dispatchEvent(new SidePaneEvent(SidePaneEvent.TOGGLE, SidePaneEvent.PANE_SEARCH));
    }

    /**
     * Sync the toggle button state.
     */
    protected function onSidePaneChange(e: SidePaneEvent): void
    {
      if (e.paneId == SidePaneEvent.PANE_SEARCH)
        m_btnSearch.selected = e.visible;
    }
  }
}
